<?php

namespace RedMVC\Db;

/**
 * ADb class
 *  
 * @category    RedMVC
 * @package     Db
 * @author      Jan Fischer, bitWorking <info@bitworking.de>
 */
abstract class ADb{

    protected $dbh;    
    protected $dbhost, $dbname, $dbusername, $dbpassword;
    protected $_type = C::TYPE_MYSQL;
    protected $_lastQuery;
    
    //abstract function setDb($dbname);
    //abstract function setCharset($charset = 'utf8');
    abstract function quote($value);
    abstract function query($query, array $args = array());
    abstract function fetch($query, array $args = array());
    abstract function fetchSelect($table, $valueField, $nameField, $selectSuffix = '');
    abstract function insert($table, array $valueArray, $onDuplicateKeyValues = null);
    abstract function update($table, array $valueArray, $whereCondition = null);
    abstract function delete($table, $whereCondition = null);
    abstract function tableExists($table);
    abstract function tableKeys($table);
    //abstract function tableFields($table);
    abstract function count($query, array $args = array());
    abstract function insertId();
    abstract function affectedRows();
    //abstract function numRows($result);
    
    abstract function beginTransaction();
    abstract function commit();
    abstract function rollBack();
    
    abstract function close();
    
    protected function _setType($dbType){
        $this->_type = $dbType;
    }
    
    public function getType(){
        return $this->_type;
    }
    
    protected function _whereCondition($whereCondition){
        $out = '';
        if(is_array($whereCondition)){
            $wheres = array();
            foreach($whereCondition as $key=>$value){
                if(null === $value){
                    $wheres[] = $key." IS NULL";
                }
                else{
                    $wheres[] = $key."=".$this->quote($value)."";
                }
            }        
            if(count($wheres) > 0){
                $out = " WHERE ".implode(' AND ', $wheres);
            }
        }
        else if(is_string($whereCondition)){
            $out = " WHERE ".$whereCondition;
        }
        return $out;
    }
    
    protected function _onDuplicateKeyQuery($onDuplicateKeyValues){
        if((null === $onDuplicateKeyValues) || (!is_array($onDuplicateKeyValues)) || (count($onDuplicateKeyValues) == 0)){
            return '';
        }
        $query      = ' ON DUPLICATE KEY UPDATE ';
        $updates    = array();
        foreach($onDuplicateKeyValues as $key => $value){
            $updates[] = $key.'='.$this->quote($value);
        }
        return $query.implode(',', $updates);
    }
    
    public function getDbh(){
        return $this->dbh;
    }
    
    public function fetchRow($query, array $args = array()){
        $rows = $this->fetch($query, $args);
        if(count($rows) > 0){
            return $rows[0];
        }
        return null;
    }
    
    public function getLastQuery(){
        return $this->_lastQuery;
    }

}